package com.example.hospital.controller.api;

import com.example.common.util.constant.HospitalConstants;
import com.example.common.util.core.controller.BaseController;
import com.example.common.util.core.resultbody.AjaxResult;
import com.example.common.util.utils.StringUtils;
import com.example.hospital.service.DepartmentService;
import com.example.hospital.service.HospitalService;
import com.example.hospital.service.ScheduleService;
import com.example.model.domain.entity.hospital.Department;
import com.example.model.domain.entity.hospital.Hospital;
import com.example.model.domain.entity.hospital.Schedule;
import com.example.service.util.HttpRequestHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author juzipi
 * @Date 2021/7/12 8:07
 * @Info
 */
@Api(tags = "api接口")
@RestController
@RequestMapping("api/hosp")
public class ApiController extends BaseController {

    @Autowired
    private HospitalService hospitalService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private ScheduleService scheduleService;


    @ApiOperation("上传医院")
    @PostMapping("saveHospital")
    public AjaxResult saveHospital(HttpServletRequest request){
        Map<String, Object> parameterMap = getParameterMap(request);
        Object hospitalLogoData = parameterMap.get(HospitalConstants.LOGO_DATA);
        String replace = hospitalLogoData.toString().replace(" ", "+");
        parameterMap.put(HospitalConstants.LOGO_DATA, replace);
        AjaxResult ajaxResult = checkSign(parameterMap);
        if (StringUtils.isNull(ajaxResult)){
            Boolean aBoolean = hospitalService.insertHospital(parameterMap);
            return toAjax(aBoolean);
        }
        return ajaxResult;
    }


    @ApiOperation(value = "查询医院")
    @PostMapping("hospital/show")
    public AjaxResult getHospital(HttpServletRequest request){
        Map<String, Object> map = getParameterMap(request);
        Object hospitalCode = map.get(HospitalConstants.HOSCODE);
        AjaxResult ajaxResult = checkSign(map);
        if (StringUtils.isNull(ajaxResult)) {
            Hospital hospital = hospitalService.getHospitalByHoscode(hospitalCode.toString());
            return AjaxResult.success(hospital);
        }
        return ajaxResult;
    }


    @ApiOperation(value = "上传科室")
    @PostMapping("saveDepartment")
    public AjaxResult saveDepartment(HttpServletRequest request){
        //获取参数，再通过工具类把String[]数组转换为Object类型
        Map<String, Object> map = getParameterMap(request);
        AjaxResult ajaxResult = checkSign(map);
        if (StringUtils.isNull(ajaxResult)){
            Boolean aBoolean = departmentService.insertDepartment(map);
            return toAjax(aBoolean);
        }
        return ajaxResult;
    }

    @ApiOperation(value = "查询科室")
    @PostMapping("department/list")
    public AjaxResult getDepartment(HttpServletRequest request){
        Map<String, Object> parameterMap = getParameterMap(request);
        //输入page值为空就赋给它一个默认值，不为空就获取page值
        Integer pageNum = Integer.parseInt(parameterMap.get(HospitalConstants.PAGE).toString());
        Integer pageSize = Integer.parseInt(parameterMap.get(HospitalConstants.LIMIT).toString());
        String hoscode = parameterMap.get(HospitalConstants.HOSCODE).toString();
        //也就是pageNum和pageSize,把操作放到service
        /*
        他的加密逻辑是这样的，有问题，应该只加密23ce3b89e0a80072a57a4977582610dd这个数据就行了，有时间了研究研究改改
        加密前：100|10|1|1620374817541|23ce3b89e0a80072a57a4977582610dd
        加密后：80823b1961b7e5204969e8754255feda
         */
        Page<Department> departmentPage = departmentService.getDepartmentPage(pageNum, pageSize, hoscode);
        return AjaxResult.success(departmentPage);
    }


    @ApiOperation(value = "删除科室")
    @PostMapping("department/remove")
    public AjaxResult removeDepartment(HttpServletRequest request){
        Map<String, Object> parameterMap = getParameterMap(request);
        String hoscode = parameterMap.get(HospitalConstants.HOSCODE).toString();
        String depcode = parameterMap.get(HospitalConstants.DEPCODE).toString();
        AjaxResult ajaxResult = checkSign(parameterMap);
//        if (StringUtils.isNull(ajaxResult)){
            Boolean aBoolean = departmentService.deleteDepartment(hoscode, depcode);
            return toAjax(aBoolean);
//        }
//        return ajaxResult;
    }


    /**
     * 上传排班
     * @param request
     * @return
     */
    @ApiOperation(value = "上传排班")
    @PostMapping("saveSchedule")
    public AjaxResult insertSchedule(HttpServletRequest request){
        Map<String, Object> parameterMap = getParameterMap(request);
        Boolean aBoolean = scheduleService.insertSchedule(parameterMap);
        return toAjax(aBoolean);
    }


    @ApiOperation(value = "查询排班")
    @PostMapping("schedule/list")
    public AjaxResult getSchedule(HttpServletRequest request){
        //跟科室一样的操作
        Map<String, Object> parameterMap = getParameterMap(request);
        Integer pageNum = Integer.parseInt(parameterMap.get(HospitalConstants.PAGE).toString());
        Integer pageSize = Integer.parseInt(parameterMap.get(HospitalConstants.LIMIT).toString());
        String hoscode = parameterMap.get(HospitalConstants.HOSCODE).toString();
        Page<Schedule> schedulePage = scheduleService.getSchedulePage(pageNum, pageSize, hoscode);
        return AjaxResult.success(schedulePage);
    }


    @ApiOperation(value = "删除排班")
    @PostMapping("schedule/remove")
    public AjaxResult removeSchedule(HttpServletRequest request){
        Map<String, Object> parameterMap = getParameterMap(request);
        String hoscode = parameterMap.get(HospitalConstants.HOSCODE).toString();
        String hpScheduleId = parameterMap.get(HospitalConstants.HOS_SCHEDULE_ID).toString();
//        AjaxResult ajaxResult = checkSign(parameterMap);
//        if (StringUtils.isNull(ajaxResult)){
        Boolean aBoolean = scheduleService.deleteSchedule(hoscode, hpScheduleId);
        return toAjax(aBoolean);
//        }
//        return ajaxResult;
    }



    /**
     * 封装成方法吧，获取请求参数
     * @param request
     * @return
     */
    private Map<String, Object> getParameterMap(HttpServletRequest request){
        Map<String, String[]> parameterMap = request.getParameterMap();
        return HttpRequestHelper.switchMap(parameterMap);
    }


    /**
     * 签名校验
     * @param parameterMap
     * @return
     */
    private AjaxResult checkSign(Map<String, Object> parameterMap) {
        Object hospitalSign = parameterMap.get(HospitalConstants.SIGN);
        Object hospitalCode = parameterMap.get(HospitalConstants.HOSCODE);
        Boolean aBoolean = hospitalService.compareHospitalSign(hospitalSign, hospitalCode);
        if (!aBoolean){
            System.out.println(aBoolean);
            return AjaxResult.error("签名校验失败");
        }
        return null;
    }


}
